const { cleanDatEnglishText } = require("../src/utils/datTextCleaner");

// 详细展示处理过程
function detailedProcess(text) {
  console.log(`原始文本: "${text}"`);

  // 第一步：处理DAT特有的格式: word1$word2$$$$$
  // 按照需求，应该把一个$和四个$之间的内容全部去掉
  let step1 = text.replace(/(\S+)\$(?:\S+)\${4,}/g, "$1");
  console.log(`第一步处理(word$word$$$$$ -> word): "${step1}"`);

  // 第二步：处理以$开头的格式: $word word$$$$$
  let step2 = step1.replace(/\$(\S+)\s*(?:\S*)\${4,}/g, "$1");
  console.log(`第二步处理($word word$$$$$ -> word): "${step2}"`);

  // 第三步：处理特殊标记
  let step3 = step2.replace(/\^/g, " ").replace(/\$/g, " ").replace(/◆/g, "");

  console.log(`第三步处理(特殊符号): "${step3}"`);

  // 第四步：分割并过滤空词
  const words = step3.split(" ").filter(word => word.trim() !== "");

  console.log(`分割后的词: [${words.map(w => `"${w}"`).join(", ")}]`);

  // 第五步：去重
  const cleanedWords = [];
  for (let i = 0; i < words.length; i++) {
    if (i < words.length - 1 && words[i].toLowerCase() === words[i + 1].toLowerCase()) {
      cleanedWords.push(words[i]);
      i++;
    } else {
      cleanedWords.push(words[i]);
    }
  }

  console.log(`去重后的词: [${cleanedWords.map(w => `"${w}"`).join(", ")}]`);

  // 第六步：处理词组重复
  let result = cleanedWords.join(" ");
  result = result.replace(/(\b\w+(?:\s+\w+)*?)\s+\1\b/gi, "$1");

  console.log(`最终结果: "${result.trim()}"`);
  console.log("---");

  return result.trim();
}

// 测试用例
const testCases = ["^products$product$$$$$", "^Please$please$$$$$^ ^give$give$$$$$^ ^our$our$$$$$^", "due to$due to$$$$$", "$you words$$$$$"];

console.log("详细处理过程演示:\n");

testCases.forEach(testCase => {
  detailedProcess(testCase);
});

console.log("\n使用现有工具处理相同内容:");
testCases.forEach(testCase => {
  const result = cleanDatEnglishText(testCase);
  console.log(`输入: "${testCase}" -> 输出: "${result}"`);
});

// 额外测试一些边界情况
console.log("\n边界情况测试:");
const edgeCases = [
  "test$testing$$$$$", // 标准格式
  "$$$$", // 只有$$$$
  "$word$$$$$", // 以$开头
  "word$$$$$", // 没有$分隔符
  "^test$testing$$$$$^", // 带^符号
];

edgeCases.forEach(testCase => {
  const result = cleanDatEnglishText(testCase);
  console.log(`输入: "${testCase}" -> 输出: "${result}"`);
});
